<template>
  <div class="app-main-container">
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearFilters"
    />
    <div v-if="meta.indexOf('SortsListAdd') > -1" class="doumee-table-buttons">
      <el-button type="primary" @click="$router.push({ name: 'SortsListEdit' })">新增</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >

      <el-table-column align="center" label="标题" prop="title" min-width="200" show-overflow-tooltip />
      <el-table-column align="center" label="列表图" prop="img2" min-width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-image
            fit="contain"
            :src="scope.row.img2"
            :preview-src-list="[scope.row.img2]"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="内容" prop="info" min-width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <span style="color:#435EBE;cursor: pointer;" @click="showBtn(scope.row)">点击查看</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="适用场馆" prop="venueNames" min-width="200" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-tooltip effect="dark" :content="scope.row.venueNames.join(',')" placement="top-start">
            <span class="han1">{{ scope.row.venueNames.join(',') }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态" min-width="120" fixed="right" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-color="#435EBE"
            inactive-color="#999999"
            :active-value="'1'"
            :inactive-value="'0'"
            :disabled="meta.indexOf('SortsListEdit') == -1"
            @change="toConfigUpdate(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="排序码" prop="sort" min-width="100" show-overflow-tooltip />
      <el-table-column align="center" label="创建时间" prop="createDate" min-width="160" />
      <el-table-column label="操作" fixed="right" align="center" min-width="150">
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button v-if="meta.indexOf('SortsListEdit') > -1" v-preventReClick type="text" @click="$router.push({ name: 'SortsListEdit',query:{ id:scope.row.id } })">编辑</el-button>
            <el-button v-if="meta.indexOf('KnowledgeDel') > -1" v-preventReClick type="text" style="color: red" @click="toDelete(scope.row.id)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- <div class="flex-cc doumee-pagination">
      <el-pagination
        background
        hide-on-single-page
        layout="prev, pager, next"
        :total="totalCount"
        @current-change="getList"
      />
    </div> -->
    <Pagination
      :total="totalCount"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
    <el-dialog
      :close-on-click-modal="modalDialog"
      :lose-on-press-escape="modalDialog"
      title="内容"
      :visible.sync="show"
    >
      <div class="fwxInfo" v-html="info" />
    </el-dialog>
  </div>
</template>

<script>
import { deleteKnowledge, listKnowledge, editKnowledge, venueList } from '@/api'
import { parseTime } from '@/utils/index'
import Pagination from '@/components/paginations'
import { queryFormConfig } from './config'
export default {
  name: 'SortsList',
  components: {
    Pagination
  },
  data() {
    return {
      modalDialog: this.$store.state.app.dialog,
      queryFormConfig,
      pagination: {
        page: 1,
        rows: 10
      },
      list: [],
      totalCount: 0,
      listLoading: true,
      filters: {},
      show: false,
      info: '',
      meta: []
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.getVenueList()
    this.getList(1)
  },
  activated() {
    if (this.$route.query.renovate) {
      this.getList(1)
    }
  },
  methods: {
    showBtn(e) {
      this.show = true
      this.info = e.info
    },
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    toDelete(id) {
      const that = this
      this.$confirm('此操作将删除该运动知识, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteKnowledge({ param: { id }}).then((res) => {
            if (res.errorCode !== '000000') return
            that.$message.success('删除成功')
            that.getList(1)
          })
        })
    },
    // 获取场馆列表
    getVenueList() {
      venueList({
        param: {}
      })
        .then((res) => {
          if (res.errorCode !== '000000') return
          const arr = res.recordList
          arr.forEach(e => {
            e.label = e.name
            e.value = e.id
          })
          this.queryFormConfig.formItems[1].options = arr
        })
    },
    toConfigUpdate(v) {
      const obj = { ...v }
      const arr = this.list
      const that = this
      this.listLoading = true
      editKnowledge({
        param: {
          id: obj.id,
          status: obj.status
        }
      })
        .then(res => {
          if (res.errorCode === '000000') {
            if (obj.status === '0') {
              that.$message.success('禁用成功')
            } else {
              that.$message.success('启用成功')
            }
            that.getList(1)
          } else {
            arr.forEach(e => {
              if (e.id === obj.id) {
                e.status = obj.status === '1' ? '0' : '1'
              }
            })
            that.list = arr
            that.listLoading = false
          }
        }).catch(() => {
          arr.forEach(e => {
            if (e.id === obj.id) {
              e.status = obj.status === '1' ? '0' : '1'
            }
          })
          that.list = arr
          that.listLoading = false
        })
    },
    getList(page) {
      const { pagination, filters: param } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (pagination.page === 1) {
        pagination.firstQueryTime = parseTime(new Date())
      }
      param.type = '1'
      this.listLoading = true
      listKnowledge({
        pagination,
        param
      }, sessionStorage.getItem('venueId')).then(res => {
        this.listLoading = false
        if (res.errorCode === '000000') {
          this.totalCount = res.totalCount
          this.list = res.recordList
        }
      }).catch(err => {
        this.listLoading = false
        console.log('-----', err)
      })
    },
    // 清空筛选项
    clearFilters() {
      this.filters = {}
      this.getList(1)
    }
  }
}
</script>

<style lang="scss" scoped>
.han1 {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
</style>
